package rjgc.mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import rjgc.mall.common.MessageConstant;
import rjgc.mall.common.MySessionContext;
import rjgc.mall.common.Result;

import rjgc.mall.entity.Seller;
import rjgc.mall.entity.User;
import rjgc.mall.entity.param.BuyParam;
import rjgc.mall.entity.vo.UserRegisterParam;
import rjgc.mall.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    HttpServletRequest request;

    /**
     * 用户购买时填写用户信息 升级包A 没有购买意向人了 购买后减去库存
     * 最后返回
     * @param buyParam
     * user       用户姓名和电话号码
     * tradeTime  交易时间 前端传进来的形式 : yyyy-MM-dd hh-mm-ss
     * tradePlace 交易地点
     * goodsId    商品编号
     * @return
     */
    @RequestMapping("/buy")
    public Result buy(@RequestHeader("userToken") String userToken,@RequestBody BuyParam buyParam) {
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        userService.buy3( user.getId(),buyParam.getTradeTime(), buyParam.getTradePlace(),buyParam.getGoodsId());
        return new Result(true, MessageConstant.GOODS_BUY_SUCCESS);
    }

    /**
     * 用户注册功能
     * @param userRegisterParam 姓名name 账号account 电话tele 默认交易地点defaultTradePlace 密码password
     * @return
     */
    @RequestMapping("/register")
    public Result register(@RequestBody UserRegisterParam userRegisterParam){
        userService.registered(userRegisterParam);
        return new Result(true,MessageConstant.USER_REGISTERED_SUCCESS);
    }

    /**
     * 用户登录
     * @param account 账号
     * @param password 密码
     * @return
     */
    @RequestMapping("/login")
    public Result login(String account,String password){
        System.out.println(account);
        User user = userService.login(account,password);
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        MySessionContext myContext = MySessionContext.getInstance();
        myContext.AddSession(session);
        return new Result(true,MessageConstant.USER_LOGIN_SUCCESS,session.getId());
    }
    //退出系统
    @RequestMapping("/logout")
    public Result logout(@RequestHeader("token") String token){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        myContext.DelSession(session);
        return new Result(true,MessageConstant.LOGOUT_SUCCESS);
    }

    //获取登录用户姓名
    @RequestMapping("/getLoginUser")
    public Result getLoginUser(@RequestHeader("token") String token){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        User user=(User) session.getAttribute("user");
        return new Result(true,MessageConstant.GET_USER_SUCCESS,user);
    }

    /**
     * 修改信息
     * @param user
     * @return
     */
    @RequestMapping("/updateInfo")
    public Result updateInfo(@RequestHeader("userToken") String userToken,@RequestBody User user){
        userService.updateInfo(user);
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        session.setAttribute("user",user);
        return new Result(true,MessageConstant.USER_UPDATE_SUCCESS);
    }

    /**
     * 获取登录成功后用户的信息
     * @param userToken
     * @return
     */
    @RequestMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader("userToken") String userToken){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        return new Result(true,"获取登录用户信息成功",user);
    }

    //新的购买商品
    @RequestMapping("/buy2")
    public Result buy2(@RequestHeader("userToken") String userToken, @RequestBody BuyParam buyParam, @RequestParam int num) {
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        userService.buy4( user.getId(),buyParam.getTradeTime(), buyParam.getTradePlace(),buyParam.getGoodsId(),num);
        return new Result(true, MessageConstant.GOODS_BUY_SUCCESS);
    }
    //新的购买商品
    /*@RequestMapping("/buy2")
    public Result buy2(@RequestParam int userid,@RequestParam String tradetime,@RequestParam String tradeplace,@RequestParam Integer goodid,@RequestParam int num) {
        userService.buy4( userid,tradetime,tradeplace,goodid,num );
        return new Result(true, MessageConstant.GOODS_BUY_SUCCESS);
    }*/
}
